<?php
namespace app\controller;

use app\BaseController;
use app\services\SystemServices;

class Index extends BaseController
{
    public function index()
    { 
        return View('',['pks'=>'id']);
    }

    //资料首页
    public function ziliao()
    {
        $post = getServices(SystemServices::class)->getMeansByConf();
        $tmp = [
            'list' => $post['data']['list'],
            'draw' => $post['data']['draw'],
            'navs' => $post['data']['navs'],
            'advs' => $post['data']['advs'],
            'ts' => $post['data']['ts'],
        ];

        $data = [
            'sig' => $post['sig'],
            'data' => encrypt($tmp, $post['sig']),
            'system' => getSystem(),
            'website' => $post['data']['website'],
        ];
      
        $data['logo'] = '/static/img/'.$data['system']['alias'].'/logo.png';
        $data['favicon'] = '/static/img/'.$data['system']['alias'].'/favicon.ico';
        
        $data['uuid'] = getFormUniqid();
        return View('',$data);
    }

    public function list()
    {
        
        return View('list/list');
    }


    //开奖记录
    public function drawp()
    {
        if(request()->isPost()) {
            $post = request()->post();
            if(empty(filtParams($post['alias']))) {
                throw new \app\CustomException('缺少参数！');
            }
            $param = [
                'alias' => filtParams($post['alias'])
            ];
            $post = getServices(SystemServices::class)->getDrawprizeList($param);
           
            $data = [
                'code'  => 200,
                'message'   => 'ok',
                'sig' => $post['sig'],
                'data' => encrypt($post['data'], $post['sig']),
            ];
            return json($data);
        }else{
            
            $post = getServices(SystemServices::class)->getLotteryCategoryList();
            $data = $post['data'];
            $data['uuid'] = getFormUniqid();
            $data['alias'] = !empty($data['list'][0]['alias']) ? $data['list'][0]['alias'] : '';
            return View('index/drawp',$data);
        }
        
    }


    //最新开奖
    public function drawprize(){
        $post = request()->post();
        if(empty(filtParams($post['alias'])) || empty($post['issue'])) {
            throw new \app\CustomException('缺少参数！');
        }

        $param = [
            'alias' => filtParams($post['alias']),
            'issue' => intval($post['issue']),
            'typeid' => 'lhc'
        ];
        $post = getServices(SystemServices::class)->getDrawprizeInfo($param);
        $data = [
            'code'  => 200,
            'message'   => 'ok',
            'sig' => $post['sig'],
            'data' => encrypt($post['data'], $post['sig']),
        ];
        return json($data);
    }


    //导航
    public function nav()
    {
        $post = getServices(SystemServices::class)->getNavs();
        $data = $post['data'];
        $data['favicon'] = '/static/img/navs/favicon.ico';
        $data['uuid'] = getFormUniqid();
        return View('nav/index',$data);
    }


    /**
     * 统计流量
     */
    public function tongji(){
        $post = request()->post();
        if(empty(filtParams($post['uuid'])) || empty(filtParams($post['newuid'])) || empty(filtParams($post['origin'])) || empty(filtParams($post['ipstring'])) || !isset($post['referer']) || !isset($post['keyword'])) {
            throw new \app\CustomException('缺少参数！');
        }
        
        $arr = [
            'uuid' => filtParams($post['uuid']),
            'newuid' => filtParams($post['newuid']),
            'host' => request()->server('HTTP_HOST'),
            'ipstring' => filtParams($post['ipstring']),
            'pathinfo' => filtParams($post['origin']),
            'referer' =>  filtParams($post['referer']),
            'keyword' =>  filtParams($post['keyword']),
        ];
    
        getServices(SystemServices::class)->flowStatistics($arr);

        $data = [
            'code'  => 200,
            'message'   => 'ok',
            'data' => [],
        ];
        return json($data);
    }



    public function message(){
        $post = request()->post();
        if(empty(filtParams($post['phone'])) || empty(filtParams($post['content']))){
            throw new \app\CustomException('缺少参数！');
        }

        $dir = __ROOT__ . '/../runtime/message/';
        $filename = $dir.date('Ymd').'.log';
        $writetext = date('Y-m-d H:i:s') . json_encode([filtParams($post['phone']),filtParams($post['content'])],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)."\n";
        if (!is_dir($dir)) {
            if (mkdir($dir, 0700) == false) {
                $data = [
                    'code'  => 500,
                    'message'   => '写入失败',
                    'data' => [],
                ];
                return json($data);
            }
        }
        if (@$fp = fopen($filename, 'a+')) {
            fwrite($fp, $writetext);
            fclose($fp);

            $data = [
                'code'  => 200,
                'message'   => '写入成功！',
                'data' => [],
            ];
            return json($data);
        } 

        $data = [
            'code'  => 500,
            'message'   => '写入失败',
            'data' => [],
        ];
        return json($data);
    }

}
